﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Windows.Data;
using System.Linq;
using Xceed.Wpf.AvalonDock.Layout;
using System.Data;
using System.Threading;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
using System.Diagnostics;
using shgoldexchange.model;
using System.Collections.Concurrent;

namespace shgoldexchange
{
    static public class InterFaceRPCStatic
    {
        public static int loginflg = 0;

        public static bool isConStaInit = false;

        static string host;
        static StreamReader sr;

        static int port = 9090;
        //static string rtnstr;
        public static userAuth Usra { get; set; } = new userAuth();
        public static DependencyObject dp;
        public static LayoutAnchorablePane ldp;
        public static List<LayoutAnchorable> listDp;
        static List<Window> listDpW;
        private static readonly object lockobj = new object();
        public static List<string> panelNames;
        public static List<string> lockids;
        private static GTPField gptfields = new GTPField();

        public static LocalDll helloClient = new LocalDll();
        static List<string> textHint;
        static List<hintDiag> hintDiags;

        public static List<string> pcxids;
        public static List<string> activeTrans;
        public static List<List<FrameworkElement>> feInitList;
        public static ConcurrentDictionary<string, ComboBox> monitorComboBoxes = new ConcurrentDictionary<string, ComboBox>();

        static List<FrameworkElement> rtnFrms;
        static DataTable tablearg = new DataTable();
        static Stopwatch watch = new Stopwatch();

        public static List<string> closeids;

        private static string lastdesid = "";
        public static Dictionary<string, FrameworkElement> rapidMap = new Dictionary<string, FrameworkElement>();
        public static DataTable dt = null;

        static List<string> runningEvents;
        public static Dictionary<string, DataTable> dicsDataTables = new Dictionary<string, DataTable>();
        public delegate void RecvInstsDoneEvent();
        public static event RecvInstsDoneEvent recvInstsDoneEvent;

        static public void thriftRPCInit(DependencyObject dptmp, LayoutAnchorablePane ldptmp)
        {

            dp = dptmp;
            ldp = ldptmp;

            panelNames = new List<string>();
            lockids = new List<string>();
            textHint = new List<string>();
            listDp = new List<LayoutAnchorable>();
            hintDiags = new List<hintDiag>();

            pcxids = new List<string>();
            activeTrans = new List<string>();
            feInitList = new List<List<FrameworkElement>>();

            rtnFrms = new List<FrameworkElement>();

            runningEvents = new List<string>();

            closeids = new List<string>();
        }

        static public servResp thriftRPCChain(string transName, LayoutAnchorable anchorable)
        {
            servResp rtn = new servResp();
            try
            {
                pTrans pt = new pTrans();
                pt.Transname = transName;
                rtn = helloClient.Chain(Usra, pt);
                pcxids.Add(rtn.Panels[0].Pctxid);
                listDp.Add(anchorable);
                closeids.Add(rtn.Panels[0].Pctxid);
                panelNames.Add("");
                lockids.Add("");
                activeTrans.Add(rtn.Panels[0].Transname);
            }
            catch (Exception ex)
            {
                ContractData.ServerLogger.Warn(ex.ToString());
            }

            if (rtn.Panels == null || rtn.Panels.Count == 0)
                return null;
            else
                return rtn;
        }

        static public bool thriftRPCLogout()
        {
            bool isLogout = false;
            servResp rtn = new servResp();

            try
            {
                //trans.Open();
                rtn = helloClient.Logout(Usra);
                if (rtn.ErrorCode == "RSP000000")
                {
                    Usra = rtn.User;
                    isLogout = true;
                }
                MessageBox.Show(rtn.ErrorCode.ToString() + rtn.User.Token);
            }
            catch (Exception ex)
            {
                ContractData.ServerLogger.Warn(ex.ToString());
            }

            return isLogout;
        }

        static public void thriftRPCPull()
        {
            ThreadPool.SetMaxThreads(3, 3);
            while (true)
            {
                servResp rtn = helloClient.Pull(Usra);
                if (rtn == null)
                    continue;

                if (rtn.ErrorCode != "RSP000000")
                {
                    continue;
                }

                //ThreadPool.QueueUserWorkItem(new WaitCallback(bgWork2), rtn.Events);
                bgWork2(rtn.Events);
            }

        }

        static public void asynPull()
        {
            while (true)
            {
                servResp rtn = helloClient.AsynPull(Usra);
                if (rtn == null)
                    continue;

                if (rtn.ErrorCode != "RSP000000")
                {
                    continue;
                }
                eComponetEventHandle(rtn.Events);
            }

        }

        static public void listData(String datasetting, String sqlstr1, String sqlsql2)
        {
			helloClient.ListData(datasetting, sqlstr1, sqlsql2);
        }

        static public bool queryKimpish(String concod)
        {
            return helloClient.QueryKmipsh(concod);
        }

        static public void chgCerPsw(String oldpwd, String newpwd)
        {
            helloClient.ChgCerPsw(oldpwd, newpwd);
        }

        static void bgWork2(object obj)
        {
            eComponetEventHandle((List<eComponet>)obj);
        }

        static public void thriftRPCall(eComponet ecom)
        {
            string key = ecom.Pctxid + ecom.Uri;
            if (ecom.Action == "ClosePanel")
                key = ecom.Pctxid + ecom.Value;
            lock (runningEvents)
            {
                if (runningEvents.Contains(key) || (ecom.Uri == "" && ecom.Action != "ClosePanel"))
                    return;
                if (ecom.Value == "" && ecom.Uri == "" && ecom.Action == "ClosePanel")
                {
                    lastdesid = ecom.Pctxid;
                    Thread.Sleep(200);
                }
                else if (ecom.Pctxid == lastdesid)
                    return;
                runningEvents.Add(key);
            }
            servResp rtn = new servResp();

            if (ecom.Uri.EndsWith("cnlbtn"))
            {
                eComponet closeCom = new eComponet();
                closeCom.Pctxid = ecom.Pctxid;
                closeCom.Action = "ClosePanel";
                closeCom.Uri = "";
                rtn.Events = new List<eComponet>();
                rtn.Events.Add(closeCom);
                eComponetEventHandle(rtn.Events);
                helloClient.DoEvent(Usra, closeCom);
                lock (runningEvents)
                {
                    if (runningEvents.Contains(key))
                        runningEvents.Remove(key);
                }
                return;
            }


            try
            {
                rtn = helloClient.DoEvent(Usra, ecom);
                int codelen = rtn.ErrorCode.Length;
                if (codelen >= 2)
                {
                    if (rtn.ErrorCode != "RSP000000")
                    {
                        if (!rtn.ErrorMessage.Equals("指定URI的对象不存在！"))
                        {
                            new Thread(new ThreadStart(delegate
                            {
                                System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                                {
                                    MessageboxShow.TopMostMessageBox.Show(rtn.ErrorMessage, "提示");
                                }));
                            })).Start();
                        }
                        eComponetEventHandle(rtn.Events);
                        lock (runningEvents)
                        {
                            if (runningEvents.Contains(key))
                                runningEvents.Remove(key);
                        }
                        return;
                    }
                }

                //watch.Restart();
                eComponetEventHandle(rtn.Events);
                //watch.Stop();
                //StreamWriter fs = new StreamWriter("aaa.txt", true);

                //fs.WriteLine("time:" + watch.ElapsedMilliseconds.ToString() + ecom.Uri);

                lock (runningEvents)
                {
                    if (runningEvents.Contains(key))
                        runningEvents.Remove(key);
                }
                //MessageBox.Show(rtn.ErrorCode.ToString());
            }
            catch (Exception ex)
            {
                lock (runningEvents)
                {
                    if (runningEvents.Contains(key))
                        runningEvents.Remove(key);
                }
                ContractData.ServerLogger.Warn(ex.ToString());
                // MessageBox.Show(ee.Message);
                //MessageBox.Show()
            }
            finally
            {
                // runningEvents.Remove(ecom.Pctxid + ecom.Uri);
            }

        }

        static private DataTable Jsonstr2DataTable(eComponet eCom)
        {
            DataTable table = new DataTable();
            try
            {
                //JsonStr为Json字符串
                JArray array = JsonConvert.DeserializeObject(eCom.Value) as JArray;//反序列化为数组

                if (array.Count > 0)
                {
                    //向表中添加数据
                    if (eCom.Uri == "mifp/miflst" && ContractData.concoddis.Count >= 0 && eCom.Pctxid == ContractData.mktinfID)  //行情列表特殊处理
                    {

                        for (int i = 0; i < array.Count; i++)
                        {
                            string concod = "";
                            foreach (JProperty jkon in (JToken)array[i])
                            {
                                string name = ((JProperty)(jkon)).Name;
                                if (name == "concod")
                                {
                                    concod = ((JProperty)(jkon)).Value.ToString();
                                    break;
                                }
                            }
                            if (ContractData.mktinfArray.ContainsKey(concod))
                            {
                                ContractData.mktinfArray[concod] = (JToken)array[i];
                            }
                            else
                            {
                                ContractData.mktinfArray.Add(concod, (JToken)array[i]);
                            }
                        }

                        JArray resArray = new JArray();
                        foreach (string str in ContractData.concoddis)
                        {
                            for (int i = 0; i < array.Count; i++)
                            {

                                string concod = "";
                                foreach (JProperty jkon in (JToken)array[i])
                                {
                                    string name = ((JProperty)(jkon)).Name;
                                    if (name == "concod")
                                    {
                                        concod = ((JProperty)(jkon)).Value.ToString();
                                        break;
                                    }
                                }
                                if (concod == str)
                                {
                                    resArray.Add(array[i]);
                                    break;
                                }

                            }
                        }
                        array = resArray;

                    }


                    for (int i = 0; i < array.Count; i++)
                    {
                        DataRow row = Json2DataTableSingle(array[i], table);
                        table.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
                ContractData.ServerLogger.Warn(ex.ToString());
            }

            return table;
        }

        static private void JsonstrAppendDataTable2(JArray array, DataTable tabletemp)
        {
            for (int i = 0; i < array.Count; i++)
            {
                DataRow row = Json2DataTableSingle(array[i], tabletemp);

                tabletemp.Rows.Add(row);
            }
        }

        static private void JsonUpdateTableSingle(JToken objColumns, DataRow row)
        {
            //JsonStr为Json字符串

            try
            {

                StringBuilder columns = new StringBuilder();
                // table = new DataTable();

                JToken obj = objColumns as JToken;
                foreach (JProperty jkon in obj)
                {

                    string name = ((JProperty)(jkon)).Name;
                    string value = ((JProperty)(jkon)).Value.ToString();
                    if (row.Table.Columns.Contains(name))
                        row[name] = value;
                    else
                    {
                        row.Table.Columns.Add(name);
                        row[name] = value;
                    }
                    //  break;
                }

            }
            catch (Exception ex)
            {
                ContractData.ServerLogger.Warn(ex.ToString());
            }
        }

        static private void JsonstrUpdateTableSingle(eComponet eCom, DataTable dt)
        {
            //JsonStr为Json字符串
            string jsonstr = eCom.Value;
            jsonstr = "[" + jsonstr + "]";
            try
            {
                JArray array = JsonConvert.DeserializeObject(jsonstr) as JArray;//反序列化为数组
                //  DataTable table = null;
                if (array.Count > 0)
                {
                    StringBuilder columns = new StringBuilder();
                    // table = new DataTable();
                    JToken objColumns = array[0] as JToken;

                    //向表中更新数据
                    JToken obj = array[0] as JToken;
                    string curConcod = "";
                    DataRow updatedr = null;
                    foreach (JProperty jkon in obj)
                    {

                        string name = ((JProperty)(jkon)).Name;
                        if (name == "concod")
                        {

                            curConcod = ((JProperty)(jkon)).Value.ToString();
                            break;
                        }
                    }
                    ContractData.mktinfArray[curConcod] = array[0] as JToken;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr["concod"].ToString() == curConcod)
                        {
                            updatedr = dr;
                            break;
                        }
                    }
                    if (updatedr == null)
                    {
                        return;
                    }
                    foreach (JProperty jkon in obj)
                    {

                        string name = ((JProperty)(jkon)).Name;
                        string value = ((JProperty)(jkon)).Value.ToString();
                        if (updatedr.Table.Columns.Contains(name))
                            updatedr[name] = value;
                        else
                        {
                            updatedr.Table.Columns.Add(name);
                            updatedr[name] = value;
                        }
                    }
                    if (dt.Columns.Contains("mardat"))
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            if (row["mardat"].ToString() != "" || row["martim"].ToString() != "")
                            {
                                PubStaticDatas.Datetime = row["mardat"].ToString() + " " + row["martim"].ToString();
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ContractData.ServerLogger.Warn(ex.ToString());
            }
        }

        static private void JsonstrUpdateTableSingle(string jsonstr, DataRow row)
        {
            //JsonStr为Json字符串
            jsonstr = "[" + jsonstr + "]";
            try
            {
                JArray array = JsonConvert.DeserializeObject(jsonstr) as JArray;//反序列化为数组
                //  DataTable table = null;
                if (array.Count > 0)
                {
                    StringBuilder columns = new StringBuilder();
                    // table = new DataTable();
                    JToken objColumns = array[0] as JToken;
                    //向表中更新数据
                    JToken obj = array[0] as JToken;
                    foreach (JProperty jkon in obj)
                    {

                        string name = ((JProperty)(jkon)).Name;
                        string value = ((JProperty)(jkon)).Value.ToString();
                        if (row.Table.Columns.Contains(name))
                            row[name] = value;
                        else
                        {
                            row.Table.Columns.Add(name);
                            row[name] = value;
                        }
                        //  break;
                    }
                }
            }
            catch (Exception ex)
            {
                ContractData.ServerLogger.Warn(ex.ToString());
            }
        }

        static int addDataTableCol(string name, DataTable table)
        {
            if ("deanum" == name || "aplnum" == name || "remnum" == name)
            {
                if (!table.Columns.Contains(name))
                    table.Columns.Add(name, System.Type.GetType("System.Decimal"));
                return 0;
            }
            else if ("aplpri" == name || "deapri" == name)
            {
                if (!table.Columns.Contains(name))
                    table.Columns.Add(name, System.Type.GetType("System.Double"));
                return 1;
            }
            else
            {
                if (!table.Columns.Contains(name))
                    table.Columns.Add(name);
                return -1;
            }
        }

        static public void UpdateRow(DataTable dt, DataRow row, int index)
        {
            try
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    dt.Rows[index][dt.Columns[i].ColumnName] = row[dt.Columns[i].ColumnName];
                }
            }
            catch (Exception ex)
            {
                ContractData.ServerLogger.Warn(ex.ToString());
            }
        }

        static public DataRow Json2DataTableSingle(JToken objColumns, DataTable table)
        {
            DataRow row = null;
            StringBuilder columns = new StringBuilder();
            //构造表头
            if (table.Columns.Count == 0)
            {
                foreach (JProperty jkon in objColumns)
                {
                    string name = ((JProperty)(jkon)).Name;
                    columns.Append(name + ",");
                    addDataTableCol(name, table);
                    //
                }
            }
            row = table.NewRow();
            foreach (JProperty jkon in objColumns)
            {

                string name = ((JProperty)(jkon)).Name;
                string value = ((JProperty)(jkon)).Value.ToString();
                if (name == "data" && value == "start")
                {

                }
                int flag = addDataTableCol(name, table);

                if (flag == 0)
                {
                    int a = 0;
                    int.TryParse(value, out a);
                    row[name] = a;
                }
                else if (flag == 1)
                {
                    double a = 0;
                    double.TryParse(value, out a);
                    row[name] = a;
                }
                else
                    row[name] = value;
            }
            return row;
        }

        static private DataRow Jsonstr2DataTableSingle(string jsonstr, DataTable table)
        {
            //JsonStr为Json字符串
            jsonstr = "[" + jsonstr + "]";
            JArray array = JsonConvert.DeserializeObject(jsonstr) as JArray;//反序列化为数组
            //  DataTable table = null;
            DataRow row = null;
            if (array.Count > 0)
            {
                return Json2DataTableSingle(array[0], table);
            }

            return row;
        }

        static private void callthrift(object obj)
        {
            InterFaceRPCStatic.thriftRPCall(obj as eComponet);
        }

        static private void eComponetEventHandleMultiStyle(object obj)
        {
            // return;
            eComponet eComtmp = obj as eComponet;
            //watch.Start();
            int index = pcxids.IndexOf(eComtmp.Pctxid);
            List<FrameworkElement> grid2List = feInitList[index];
            //FrameworkElement grid2 = feInitList[index];
            FrameworkElement objfw;
            //watch.Stop();
            //PrintLogicalTree(0, grid1);
            //MessageBox.Show(rtnstr);
            //string bb = "";
            //string aa = "";
            //string cc = "";
            //cc = watch.ElapsedMilliseconds.ToString();
            if (eComtmp.Uri.Contains("#"))
            {
                //调试event
                string listvh = eComtmp.Uri.Substring(eComtmp.Uri.LastIndexOf("/") + 1);
                string realpath = eComtmp.Uri.Substring(0, eComtmp.Uri.LastIndexOf("/"));
                string[] listvhs = listvh.Split('#');
                int v = int.Parse(listvhs[0]);
                string h = listvhs[1];
                FrameworkElement objfw2 = null;
                if (rapidMap.ContainsKey(realpath + eComtmp.Pctxid))
                    objfw2 = rapidMap[realpath + eComtmp.Pctxid];
                else
                    objfw2 = FindeComponetByList(grid2List, realpath.Replace("/", "_"), eComtmp.Pctxid);

                if (objfw2 is DataGrid)
                {
                    //watch.Restart();
                    int columnIndex = 0;
                    objfw2.Dispatcher.Invoke(new Action(() =>
                    {
                        for (int i = 0; i < ((DataGrid)objfw2).Columns.Count; i++)
                        {
                            DataGridTextColumn dtc = ((DataGrid)objfw2).Columns[i] as DataGridTextColumn;
                            if (dtc == null)
                                continue;
                            Binding dgtcb = dtc.Binding as Binding;

                            if (dgtcb.Path.Path == h)
                            {
                                columnIndex = i;
                            }
                        }
                    }));
                    DataGrid grid = ((DataGrid)objfw2);
                    if (!dicsDataTables.ContainsKey(realpath + eComtmp.Pctxid))
                        return;
                    DataTable table = dicsDataTables[realpath + eComtmp.Pctxid];
                    DataRow row = table.Rows[v];
                    int realidx = 0;
                    for (int i = 0; i < grid.Items.Count; i++)
                    {
                        System.Data.DataRowView objjj = (System.Data.DataRowView)grid.Items[i];
                        if (objjj.Row == row)
                            break;
                        else
                            realidx++;
                    }
                    objfw2.Dispatcher.Invoke(new Action(() =>
                    {
                        try
                        {
                            //foreach (System.Windows.Controls.ItemsControl item in grid.Items)
                            //{
                            //   object obj2222=item;
                            //}
                            DataGridRow dgr = (DataGridRow)(((DataGrid)objfw2).ItemContainerGenerator.ContainerFromIndex(realidx));
                            if (dgr == null)
                            {
                                ((DataGrid)objfw2).UpdateLayout();
                                ((DataGrid)objfw2).ScrollIntoView(((DataGrid)objfw2).Items[realidx]);
                                dgr = (DataGridRow)(((DataGrid)objfw2).ItemContainerGenerator.ContainerFromIndex(realidx));
                            }
                            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(dgr);

                            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                            cell.SetResourceReference(DataGridCell.StyleProperty, eComtmp.Value.Substring(1));
                        }
                        catch (Exception ex)
                        {
                            ContractData.ServerLogger.Warn(ex.ToString());
                        }
                    }));
                    //watch.Stop();
                    //aa = watch.ElapsedMilliseconds.ToString();
                }

            }
            else
            {
                FrameworkElement objfw2 = FindeComponetByList(grid2List, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);

                if (objfw2 is Button)
                {
                    objfw2.Dispatcher.Invoke(new Action(() =>
                    {
                        ((Button)objfw2).SetResourceReference(Button.StyleProperty, eComtmp.Value.Substring(1));
                    }));

                }
            }
            //rtnstr = "";
            //objfw = FindeComponet(grid2, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
            //if (objfw is TextBox)
            //{
            //    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
            //    {
            //        ((TextBox)objfw).SetResourceReference(TextBox.StyleProperty, eComtmp.Value);
            //    }));
            //}



            //StreamWriter fs = new StreamWriter("aaa.txt", true);

            //fs.WriteLine("lanbuda1:" + bb);
            //fs.WriteLine("FindeComponet:" + cc);
            //fs.WriteLine("lanbuda2:" + aa);
            //fs.Close();
        }

        static private void eComponetEventHandleMulti(object obj)
        {
            eComponet eComtmp = obj as eComponet;
            string streve = eComtmp.Action;
            if (streve == "setModify")
            {
                FrameworkElement objfw = FindeComponet(dp, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                //PrintLogicalTree(0, grid1);
                //MessageBox.Show(rtnstr);
                if (objfw == null)
                {
                    //调试event
                    //MessageBox.Show("event无法处理:" + eComtmp.Uri);
                    return;
                }
                //rtnstr = "";
                if (objfw is TextBox)
                {
                    objfw.Dispatcher.Invoke(new Action(() =>
                    {
                        ((TextBox)objfw).Text = eComtmp.Value;
                    }));
                }
                else if (objfw is DataGrid)
                {
                    if (eComtmp.Value == "")
                    {
                        objfw.Dispatcher.Invoke(new Action(() =>
                        {
                            ((DataGrid)objfw).ItemsSource = null;
                        }));

                        return;
                    }

                    //List<List<string>> listviewdata = new List<List<string>>();
                    //DataTable dt = new DataTable();
                    //var jalist = JArray.Parse(eComtmp.Value);

                    //foreach (JObject jo in jalist)
                    //{
                    //    List<string> itemList = new List<string>();
                    //    foreach (var item in jo)
                    //    {
                    //        itemList.Add(item.Value.ToString());
                    //    }
                    //    listviewdata.Add(itemList);
                    //}

                    objfw.Dispatcher.Invoke(new Action(() =>
                    {




                        //for (int i = 0; i < listviewdata.Count; i++)
                        //{
                        //DataRow dr = dt.NewRow();

                        //dr["Gender"] = "b";
                        //dr["CurPlace"] = "c";

                        ////}
                        //dt.Rows.Add(dr);

                        ((DataGrid)objfw).ItemsSource = Jsonstr2DataTable(eComtmp).DefaultView;
                    }));
                }
                else if (objfw is ListView)
                {
                    if (eComtmp.Value == "")
                    {
                        objfw.Dispatcher.Invoke(new Action(() =>
                        {
                            ((ListView)objfw).ItemsSource = null;
                        }));

                        return;
                    }
                    List<List<string>> listviewdata = new List<List<string>>();
                    List<string> listHead = new List<string>();
                    bool isGetHead = false;
                    objfw.Dispatcher.Invoke(new Action(() =>
                    {
                        if ((((ListView)objfw).View as GridView).Columns.Count > 0)
                            isGetHead = true;
                    }));

                    var jalist = JArray.Parse(eComtmp.Value);

                    foreach (JObject jo in jalist)
                    {
                        List<string> itemList = new List<string>();
                        foreach (var item in jo)
                        {
                            if (!isGetHead)
                                listHead.Add(item.Key);
                            itemList.Add(item.Value.ToString());
                        }
                        isGetHead = true;
                        listviewdata.Add(itemList);
                    }

                    objfw.Dispatcher.Invoke(new Action(() =>
                    {
                        ((ListView)objfw).ItemsSource = listviewdata;
                        //((DataGrid)objfw).ItemsSource = listviewdata;
                    }));

                    foreach (string headstr in listHead)
                    {
                        objfw.Dispatcher.Invoke(new Action(() =>
                        {
                            GridViewColumn columnC = new GridViewColumn();
                            GridViewColumnHeader columnHead = new GridViewColumnHeader();

                            columnHead.Content = headstr;
                            columnHead.Tag = headstr;
                            columnC.Header = columnHead;

                            columnC.DisplayMemberBinding = new Binding(".[" + listHead.IndexOf(headstr).ToString() + "]");



                            (((ListView)objfw).View as GridView).Columns.Add(columnC);
                        }));

                    }

                }
                else
                {
                    //   MessageBox.Show(eComtmp.Uri + " is not exist.");
                }
            }
            else if (streve == "setStyle")
            {
                FrameworkElement objfw = FindeComponet(dp, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                //PrintLogicalTree(0, grid1);
                //MessageBox.Show(rtnstr);
                if (objfw == null)
                {
                    //调试event
                    string listvh = eComtmp.Uri.Substring(eComtmp.Uri.LastIndexOf("/") + 1);
                    string realpath = eComtmp.Uri.Substring(0, eComtmp.Uri.LastIndexOf("/"));
                    string[] listvhs = listvh.Split('#');
                    int v = int.Parse(listvhs[0]);
                    string h = listvhs[1];
                    FrameworkElement objfw2 = FindeComponet(dp, realpath.Replace("/", "_"), eComtmp.Pctxid);
                    if (objfw2 is DataGrid)
                    {
                        int columnIndex = 0;
                        objfw2.Dispatcher.Invoke(new Action(() =>
                        {
                            for (int i = 0; i < ((DataGrid)objfw2).Columns.Count; i++)
                            {
                                Binding dgtcb = ((DataGridTextColumn)(((DataGrid)objfw2).Columns[i])).Binding as Binding;

                                if (dgtcb.Path.Path == h)
                                {
                                    columnIndex = i;
                                }
                            }
                        }));

                        objfw2.Dispatcher.Invoke(new Action(() =>
                        {
                            DataGridRow dgr = (DataGridRow)(((DataGrid)objfw2).ItemContainerGenerator.ContainerFromIndex(v));
                            if (dgr == null)
                            {
                                ((DataGrid)objfw2).UpdateLayout();
                                ((DataGrid)objfw2).ScrollIntoView(((DataGrid)objfw2).Items[v]);
                                dgr = (DataGridRow)(((DataGrid)objfw2).ItemContainerGenerator.ContainerFromIndex(v));
                            }
                            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(dgr);

                            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
                            cell.SetResourceReference(DataGridCell.StyleProperty, eComtmp.Value.Substring(1));
                        }));
                    }
                }
                //rtnstr = "";
                if (objfw is TextBox)
                {
                    objfw.Dispatcher.Invoke(new Action(() =>
                    {
                        ((TextBox)objfw).SetResourceReference(TextBox.StyleProperty, eComtmp.Value);
                    }));
                }
            }
            else if (streve == "direct")
            {
                System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    if (!panelNames.Contains(eComtmp.Uri.Replace("/", "_")))
                    {
                        LayoutAnchorable anchorable = new LayoutAnchorable();
                        anchorable.ContentId = eComtmp.Pctxid;
                        Grid grid2 = new Grid();
                        object xamlUI = Activator.CreateInstance(Type.GetType("shgoldexchange." + eComtmp.Value), false);
                        panelNames.Add(eComtmp.Uri.Replace("/", "_"));



                        grid2.Children.Add(xamlUI as UserControl);
                        SetTagComponet(grid2, typeof(Button), eComtmp.Pctxid);
                        SetTagComponet(grid2, typeof(ListView), eComtmp.Pctxid);
                        SetTagComponet(grid2, typeof(TextBox), eComtmp.Pctxid);
                        SetTagComponet(grid2, typeof(PasswordBox), eComtmp.Pctxid);
                        SetTagComponet(grid2, typeof(DataGrid), eComtmp.Pctxid);
                        anchorable.Content = grid2;
                        anchorable.FloatingWidth = 600;
                        anchorable.FloatingHeight = 300;
                        anchorable.FloatingTop = 200;
                        anchorable.FloatingLeft = 300;

                        listDp.Add(anchorable);
                        ldp.Children.Add(anchorable);
                        anchorable.Closing += anchorable_Closing;
                        anchorable.Float();     //调用Float方法，使窗体浮动显示
                    }
                    else
                    {
                        ldp.Children[panelNames.IndexOf(eComtmp.Uri.Replace("/", "_"))].Float();
                    }


                }));
            }
            else if (streve == "popup")
            {
                try {
                    messageBoxPopup mbp;

                    mbp = JsonConvert.DeserializeObject<messageBoxPopup>(eComtmp.Value);
                    if (eComtmp.Uri == "")
                    {
                        //MessageBox.Show(eComtmp.Value);

                        new Thread(new ThreadStart(delegate
                        {
                            try
                            {
                                string resultstr = mbp.popup(mbp.messtyle);
                                eComponet ecom = new eComponet();
                                ecom.Action = "ClosePanel";
                                ecom.Uri = "";
                                ecom.Value = resultstr + "," + mbp.lockid;
                                ecom.Pctxid = eComtmp.Pctxid;
                                InterFaceRPCStatic.thriftRPCall(ecom);
                            } catch (Exception ex)
                            {
                                ContractData.ServerLogger.Warn(ex.ToString());
                            }
                        })).Start();
                    }
                    else
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            if (!panelNames.Contains(eComtmp.Uri.Replace("/", "_")))
                            {
                                LayoutAnchorable anchorable = new LayoutAnchorable();

                                anchorable.ContentId = eComtmp.Pctxid;
                                Grid grid2 = new Grid();
                                object xamlUI = Activator.CreateInstance(Type.GetType("shgoldexchange." + mbp.message.Replace("/", "_")), false);

                                panelNames.Add(eComtmp.Uri.Replace("/", "_"));
                                lockids.Add(mbp.lockid);
                                grid2.Children.Add(xamlUI as UIElement);
                                SetTagComponet(grid2, typeof(Button), eComtmp.Pctxid);
                                SetTagComponet(grid2, typeof(ListView), eComtmp.Pctxid);
                                SetTagComponet(grid2, typeof(TextBox), eComtmp.Pctxid);
                                SetTagComponet(grid2, typeof(DataGrid), eComtmp.Pctxid);
                                SetTagComponet(grid2, typeof(PasswordBox), eComtmp.Pctxid);
                                anchorable.Content = grid2;
                            //w1.Content = grid2;
                            //w1.Tag = eComtmp.Uri.Replace("/", "_");
                            //listDpW.Add(w1);
                            //w1.ShowDialog();
                            //w1.Closed += w1_Closed;
                            anchorable.FloatingWidth = 600;
                                anchorable.FloatingHeight = 500;
                                anchorable.FloatingTop = 200;
                                anchorable.FloatingLeft = 300;
                                anchorable.Title = "交易所公告";

                                listDp.Add(anchorable);

                                ContractData.jyspubPane.Children.Add(anchorable);
                                anchorable.Closing += anchorable_PopupClosing;
                            //anchorable.Float();     //调用Float方法，使窗体浮动显示
                        }
                            else
                            {
                                ldp.Children[panelNames.IndexOf(eComtmp.Uri.Replace("/", "_"))].Float();
                            }


                        }));

                    }
                }
                catch(Exception ex)
                { ContractData.ServerLogger.Warn(ex.ToString()); }
            }
            else if (streve == "ClosePanel")
            {
                if (panelNames.Contains(eComtmp.Uri.Replace("/", "_")))
                {
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        //listDp[panelNames.IndexOf(eComtmp.Uri.Replace("/", "_"))].DockAsDocument();
                        System.Windows.Application.Current.MainWindow.Focus();// MainWindow.Activate();
                        System.Windows.Application.Current.MainWindow.Activate();
                        listDpW[0].Close();
                        System.Windows.Application.Current.MainWindow.Focus();// MainWindow.Activate();
                        System.Windows.Application.Current.MainWindow.Activate();
                    }));
                }
            }
            else if (streve == "OnClick")
            {
                string tmpUri = eComtmp.Uri;
                string transUriName = tmpUri.Substring(0, tmpUri.IndexOf(":"));
                eComtmp.Uri = tmpUri.Substring(tmpUri.IndexOf(":") + 1);
                eComtmp.Pctxid = pcxids[activeTrans.IndexOf(transUriName.Trim())];

                //Thread.Sleep(2000);
                Thread eventThread;
                eventThread = new Thread(new ParameterizedThreadStart(callthrift));
                eventThread.SetApartmentState(ApartmentState.STA);
                eventThread.IsBackground = true;
                eventThread.Start(eComtmp);
            }
            else if (streve == "ERROR")
            {
                int hintindex = textHint.IndexOf(eComtmp.Uri.Replace("/", "_"));
                if (hintindex < 0)
                {
                    FrameworkElement objfw = FindeComponet(dp, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                    //PrintLogicalTree(0, grid1);
                    //MessageBox.Show(rtnstr);
                    if (objfw == null)
                    {
                        //调试event
                        //MessageBox.Show("event无法处理:" + eComtmp.Uri);
                        return;
                    }
                    //rtnstr = "";
                    if (objfw is TextBox)
                    {
                        //hintDiag hd = new hintDiag();
                        //hd.ShowInTaskbar = false;
                        //hintDiags.Add(hd);

                        ////Point pt  = ((TextBox)objfw).TranslatePoint(new Point(0, 0), (UIElement)dp);
                        //Point pt = ((TextBox)objfw).PointToScreen(new Point(0, 0));
                        //hd.Left = pt.X + ((TextBox)objfw).Width;
                        //hd.Top = pt.Y;
                        //textHint.Add(eComtmp.Uri.Replace("/", "_"));
                        //hd.showText(eComtmp.Value);
                        ((TextBox)objfw).ToolTip = eComtmp.Value;
                    }
                    if (objfw is PasswordBox)
                    {
                        ((PasswordBox)objfw).ToolTip = eComtmp.Value;
                    }


                }
                else
                {
                    //hintDiag hd = hintDiags[hintindex];
                    //hd.Show();
                }
            }
            else
            {
                thriftRPCall(eComtmp);
            }
        }

        private static void ReloadMonitorCombobox()
        {
            dp.Dispatcher.Invoke(new Action(() =>
            {
                if(monitorComboBoxes.Values != null && monitorComboBoxes.Values.Any())
                {
                    foreach(var cb in monitorComboBoxes.Values)
                    {
                        cb.RaiseEvent(new RoutedEventArgs(ComboBox.LoadedEvent));
                    }
                }
            }));
        }

        private static void handleInstInfos(string instinfos)
        {
            InstModel inst = JsonConvert.DeserializeObject<InstModel>(instinfos);
            InstModelCollection.updateInstInfo(inst);
            ReloadMonitorCombobox();
        }

        private static void handleVarietyInfos(string varInfos)
        {
            recvInstsDoneEvent?.Invoke();
            VarietyModel variety = JsonConvert.DeserializeObject<VarietyModel>(varInfos);
            VarietyModelCollection.updateVarietyInfo(variety);
        }

        private static void handleMarketInfos(string marInfos)
        {
            MarketModel market = JsonConvert.DeserializeObject<MarketModel>(marInfos);
            MarketModelCollection.updateMarketInfo(market);
        }

        private static void handleBaseData(eComponet eCom)
        {
            try {
                if (eCom.Uri.Equals("instInfos"))
                {
                    handleInstInfos(eCom.Value);
                }
                else if (eCom.Uri.Equals("varietyInfos"))
                {
                    handleVarietyInfos(eCom.Value);
                }
                else if (eCom.Uri.Equals("marketInfos"))
                {
                    handleMarketInfos(eCom.Value);
                }
                else
                {
                    ContractData.ClientLogger.Warn("unknown type msg, uri:" + eCom.Uri + " value:" + eCom.Value);
                }
            }
            catch(Exception ex)
            {
                ContractData.ClientLogger.Error("handleBaseData exception, ex:" + ex.ToString());
            }
        }

        public static void eComponetEventHandle(List<eComponet> eComs)
        {
            //lock (lockobj)
            {
                if (eComs == null)
                    return;
                try
                {
                    //DataTable dtnew = new DataTable();
                    List<string> dtnewListName = new List<string>();
                    List<DataTable> dtnewList = new List<DataTable>();
                    Dictionary<String, DataTable> map = new Dictionary<string, DataTable>();
                    Dictionary<String, FrameworkElement> ctxmap = new Dictionary<string, FrameworkElement>();
                    foreach (eComponet eComtmp in eComs)
                    {
                        if (eComtmp.Uri.Contains("sfep/sfelst"))
                        {
                            ContractData.ClientLogger.Debug("强平返回数据sfep/sfelst：" + eComtmp.Value);
                        }
                        string streve = eComtmp.Action;
                        if(streve.Equals("BaseData"))
                        {
                            handleBaseData(eComtmp);
                            continue;
                        }
                        else  if (streve == "setModify")
                        {
                            int index = pcxids.IndexOf(eComtmp.Pctxid);
                            List<FrameworkElement> grid2List = feInitList[index];
                            //FrameworkElement grid2 = feInitList[index];
                            FrameworkElement objfw = null;
                            FrameworkElement objfw1 = null;
                            if (eComtmp.Uri.EndsWith("#"))
                            {
                                string realpath = eComtmp.Uri.Substring(0, eComtmp.Uri.LastIndexOf("/"));
                                if (realpath == "")
                                {
                                    continue;
                                }
                                if (realpath == "tbap/mstlst")
                                {
                                    DataTable dttmp = ContractData.constaData;
                                    lock (lockobj)
                                    {
                                        string jsonstr = "[" + eComtmp.Value + "]";
                                        try
                                        {
                                            JArray array = JsonConvert.DeserializeObject(jsonstr) as JArray;//反序列化为数组
                                            if (array.Count == 0)
                                                continue;
                                            bool flag = false;
                                            string key = (string)array[0]["tittle"];
                                            string inr = (string)array[0]["inr"];
                                            if (dttmp.Rows.Count > 0)
                                            {
                                                DataRow[] rows = dttmp.Select("tittle='" + key + "'");
                                                if (rows.Length > 0)
                                                {
                                                    string oinr = (string)rows[0]["inr"];
                                                }
                                                else
                                                {
                                                    dttmp.Rows.InsertAt(Jsonstr2DataTableSingle(eComtmp.Value, dttmp), 0);
                                                }
                                            }
                                            else
                                            {
                                                dttmp.Rows.InsertAt(Jsonstr2DataTableSingle(eComtmp.Value, dttmp), 0);
                                            }

                                        }
                                        catch (Exception ex)
                                        { ContractData.ServerLogger.Warn(ex.ToString()); }
                                    }
                                    continue;
                                }
                                string rowNum = eComtmp.Uri.Substring(eComtmp.Uri.LastIndexOf("/") + 1, eComtmp.Uri.IndexOf("#") - eComtmp.Uri.LastIndexOf("/") - 1);

                                int rowIdx = int.Parse(rowNum);
                                int datagridindex = -1;
                                if (rapidMap.ContainsKey(realpath + eComtmp.Pctxid))
                                    objfw = rapidMap[realpath + eComtmp.Pctxid];
                                else
                                    objfw = FindeComponetByList(grid2List, realpath.Replace("/", "_"), eComtmp.Pctxid);
                                if (objfw == null)
                                    continue;


                                objfw.Dispatcher.Invoke(new Action(() =>
                                {
                                    lock (objfw)
                                    {
                                        try
                                        {
                                            if (dicsDataTables.ContainsKey(realpath + eComtmp.Pctxid))
                                            {
                                                DataTable dttmp = dicsDataTables[realpath + eComtmp.Pctxid];
                                                //dttmp.
                                                string keycod = "";
                                                if (rapidMap.ContainsKey(realpath + eComtmp.Pctxid))
                                                {
                                                    if (eComtmp.Uri.Contains("rdsp/cptlst") || eComtmp.Uri.Contains("seep/seelst") || eComtmp.Uri.Contains("mhdp/mhdlst"))
                                                        keycod = "ordcod";
                                                    else if (eComtmp.Uri.Contains("rdsp/ccmlst") || eComtmp.Uri.Contains("seep/sdelst") || eComtmp.Uri.Contains("mhdp/mselst")) //成交编号+买卖
                                                        keycod = "deacod";
                                                }

                                                try
                                                {
                                                    if (realpath == "rdsp/cptlst" || realpath == "rdsp/ccmlst" || realpath == "mhdp/mhdlst" || realpath == "mhdp/mselst" || realpath == "seep/seelst" || realpath == "seep/sdelst")
                                                    {
                                                        DateTime t0 = DateTime.Now;

                                                        JArray array = JsonConvert.DeserializeObject(eComtmp.Value) as JArray;//反序列化为数组
                                                        DateTime t1 = DateTime.Now;
                                                        TimeSpan ts = t1 - t0;
                                                        string info = "json反序列化（该组有" + array.Count.ToString() + "条数据）耗时：" + ts.TotalMilliseconds + "ms";
                                                        //ContractData.logInstance.Debug(info);

                                                        int curCount = 0;
                                                        int listindex = 0;
                                                        if (realpath == "rdsp/cptlst")
                                                        {
                                                            listindex = 1;
                                                        }
                                                        else if (realpath == "rdsp/ccmlst")
                                                        {
                                                            listindex = 2;
                                                        }
                                                        else if (realpath == "mhdp/mhdlst")
                                                        {
                                                            listindex = 5;
                                                        }
                                                        else if (realpath == "mhdp/mselst")
                                                        {
                                                            listindex = 6;
                                                        }
                                                        else if (realpath == "seep/seelst")
                                                        {
                                                            listindex = 3;
                                                        }
                                                        else if (realpath == "seep/sdelst")
                                                        {
                                                            listindex = 4;
                                                        }
                                                        curCount = Int32.Parse(ContractData.listCurCount[listindex.ToString()]);

                                                        dttmp.BeginLoadData();

                                                        for (int i = 0; i < array.Count; i++)
                                                        {
                                                            if (array[i].ToString().Contains("data") && array[i].ToString().Contains("start"))
                                                            {

                                                                foreach (JProperty jkon in array[i])
                                                                {
                                                                    string name = ((JProperty)(jkon)).Name;
                                                                    if (name == "data")
                                                                    {
                                                                        string value = ((JProperty)(jkon)).Value.ToString();
                                                                        string count = value.Substring(5);
                                                                        string[] arrayCount = count.Split('#');
                                                                        string totalCount = "0";
                                                                        string currCount = "0";
                                                                        if (arrayCount.Length == 2)
                                                                        {
                                                                            totalCount = arrayCount[0].ToString();
                                                                            currCount = arrayCount[1].ToString();
                                                                        }
                                                                        //设置滑块与滚动条布局
                                                                        if (listindex == 1)
                                                                        {
                                                                            objfw1 = FindeComponetByList(grid2List, "rdsp_scrollBar1", eComtmp.Pctxid);
                                                                            ContractData.infoRecord.IntValue1 = Int32.Parse(totalCount);
                                                                        }
                                                                        if (listindex == 2)
                                                                        {
                                                                            objfw1 = FindeComponetByList(grid2List, "rdsp_scrollBar2", eComtmp.Pctxid);
                                                                            ContractData.infoRecord.IntValue2 = Int32.Parse(totalCount);
                                                                        }
                                                                        if (listindex == 3)
                                                                        {
                                                                            objfw1 = FindeComponetByList(grid2List, "seep_scrollBar1", eComtmp.Pctxid);
                                                                            ContractData.infoRecord.IntValue3 = Int32.Parse(totalCount);
                                                                        }
                                                                        if (listindex == 4)
                                                                        {
                                                                            objfw1 = FindeComponetByList(grid2List, "seep_scrollBar2", eComtmp.Pctxid);
                                                                            ContractData.infoRecord.IntValue4 = Int32.Parse(totalCount);
                                                                        }
                                                                        if (listindex == 5)
                                                                        {
                                                                            objfw1 = FindeComponetByList(grid2List, "mhdp_scrollBar1", eComtmp.Pctxid);
                                                                            ContractData.infoRecord.IntValue5 = Int32.Parse(totalCount);
                                                                        }
                                                                        if (listindex == 6)
                                                                        {
                                                                            objfw1 = FindeComponetByList(grid2List, "mhdp_scrollBar2", eComtmp.Pctxid);
                                                                            ContractData.infoRecord.IntValue6 = Int32.Parse(totalCount);
                                                                        }


                                                                        ScrollBar sb = (ScrollBar)objfw1;
                                                                        sb.Maximum = Int32.Parse(totalCount) - Int32.Parse(ContractData.listViewCount[listindex.ToString()]);
                                                                        ContractData.listIndex[listindex.ToString()] = currCount.ToString();
                                                                        sb.Value = Int32.Parse(currCount);
                                                                        ContractData.listTotalCount[listindex.ToString()] = sb.Maximum.ToString();
                                                                        if (listindex == 1)
                                                                        {
                                                                            string infomation = "报单列表设置数据总量：" + sb.Maximum.ToString() + ",当前数据位置：" + currCount.ToString();
                                                                            //ContractData.logInstance.Debug(infomation);
                                                                        }

                                                                        break;
                                                                    }
                                                                }
                                                                curCount = 0;
                                                                continue;
                                                            }
                                                            if (array[i].ToString().Contains("data2") && array[i].ToString().Contains("end"))  //是该组报文的结尾
                                                            {
                                                                ContractData.listCurCount[listindex.ToString()] = "0";
                                                                while (dttmp.Rows.Count > curCount)  //删除上一页多余行
                                                                {
                                                                    dttmp.Rows.Remove(dttmp.Rows[dttmp.Rows.Count - 1]);
                                                                }

                                                                continue;
                                                            }

                                                            if (dttmp.Rows.Count <= 0)  //datatable没有数据
                                                            {

                                                                dttmp.Rows.InsertAt(Json2DataTableSingle(array[i], dttmp), dttmp.Rows.Count);
                                                                curCount++;
                                                            }
                                                            else
                                                            {
                                                                if (dttmp.Rows.Count > curCount)  //更新数据
                                                                {
                                                                    //DataRow newRow = Json2DataTableSingle(array[i], dttmp);
                                                                    //UpdateRow(dttmp, newRow, curCount);
                                                                    JsonUpdateTableSingle(array[i], dttmp.Rows[curCount]);
                                                                }
                                                                else    //插入数据
                                                                {
                                                                    dttmp.Rows.InsertAt(Json2DataTableSingle(array[i], dttmp), curCount);
                                                                }
                                                                curCount++;
                                                            }
                                                            if (listindex != 0)   //将最新的数据表count保存
                                                            {
                                                                ContractData.listCurCount[listindex.ToString()] = curCount.ToString();
                                                            }
                                                            //info = "解析一条数据完成：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                                                            //ContractData.logInstance.Debug(info);

                                                        }
                                                        dttmp.EndLoadData();
                                                        return;
                                                    }
                                                }
                                                catch (Exception ex)
                                                { ContractData.ServerLogger.Warn(ex.ToString()); }


                                                try
                                                {


                                                    if (eComtmp.Uri.Contains("+"))
                                                    {

                                                        dttmp.Rows.InsertAt(Jsonstr2DataTableSingle(eComtmp.Value, dttmp), rowIdx);
                                                    }
                                                    else if (eComtmp.Uri.Contains("-"))
                                                    {
                                                        rowIdx = -rowIdx;
                                                        dttmp.Rows.RemoveAt(rowIdx);
                                                    }
                                                    else
                                                    {
                                                        if (eComtmp.Uri.Contains("mifp/miflst")) //行情更新推送
                                                        {
                                                            JsonstrUpdateTableSingle(eComtmp, dttmp);

                                                        }
                                                        else
                                                        {
                                                            //更新数据
                                                            if (rowIdx < dttmp.Rows.Count)
                                                            {

                                                                JsonstrUpdateTableSingle(eComtmp.Value, dttmp.Rows[rowIdx]);
                                                            }
                                                        }
                                                    }
                                                }
                                                catch (Exception ex)
                                                {
                                                    ContractData.ServerLogger.Warn(ex.ToString());
                                                }
                                                //}
                                                lock (rapidMap)
                                                {
                                                    //if (!rapidMap.ContainsKey(realpath + eComtmp.Pctxid))
                                                    //    rapidMap.Add(realpath + eComtmp.Pctxid, objfw);
                                                }
                                            }
                                            else
                                            {

                                                if (rapidMap.ContainsKey(realpath + eComtmp.Pctxid))
                                                    objfw = rapidMap[realpath + eComtmp.Pctxid];
                                                else
                                                    objfw = FindeComponetByList(grid2List, realpath, eComtmp.Pctxid);

                                                if (realpath == "rdsp/cptlst" || realpath == "rdsp/ccmlst" || realpath == "mhdp/mhdlst" || realpath == "mhdp/mselst" || realpath == "seep/seelst" || realpath == "seep/sdelst")
                                                {
                                                    DataTable dttmp = new DataTable();
                                                    DateTime t0 = DateTime.Now;

                                                    JArray array = JsonConvert.DeserializeObject(eComtmp.Value) as JArray;//反序列化为数组
                                                    DateTime t1 = DateTime.Now;
                                                    TimeSpan ts = t1 - t0;
                                                    string info = "json反序列化（该组有" + array.Count.ToString() + "条数据）耗时：" + ts.TotalMilliseconds + "ms";
                                                    //ContractData.logInstance.Debug(info);

                                                    bool endflg = false;   //非第一页结尾标志(跳页)
                                                    bool end1flg = false;  //第一页结尾标志
                                                    bool end2flg = false;  //向后翻页结尾标志
                                                    bool end0flg = false;  //向前翻页结尾标志
                                                    bool end3flg = false;  //页数不改变标志
                                                    bool end4flg = false;  //最后一页结尾标志


                                                    int curCount = 0;
                                                    int listindex = 0;
                                                    if (realpath == "rdsp/cptlst")
                                                    {
                                                        listindex = 1;
                                                    }
                                                    else if (realpath == "rdsp/ccmlst")
                                                    {
                                                        listindex = 2;
                                                    }
                                                    else if (realpath == "mhdp/mhdlst")
                                                    {
                                                        listindex = 5;
                                                    }
                                                    else if (realpath == "mhdp/mselst")
                                                    {
                                                        listindex = 6;
                                                    }
                                                    else if (realpath == "seep/seelst")
                                                    {
                                                        listindex = 3;
                                                    }
                                                    else if (realpath == "seep/sdelst")
                                                    {
                                                        listindex = 4;
                                                    }
                                                    curCount = Int32.Parse(ContractData.listCurCount[listindex.ToString()]);

                                                    dttmp.BeginLoadData();

                                                    for (int i = 0; i < array.Count; i++)
                                                    {
                                                        if (array[i].ToString().Contains("data") && array[i].ToString().Contains("start"))
                                                        {

                                                            foreach (JProperty jkon in array[i])
                                                            {
                                                                string name = ((JProperty)(jkon)).Name;
                                                                if (name == "data")
                                                                {
                                                                    string value = ((JProperty)(jkon)).Value.ToString();
                                                                    string count = value.Substring(5);
                                                                    string[] arrayCount = count.Split('#');
                                                                    string totalCount = "0";
                                                                    string currCount = "0";
                                                                    if (arrayCount.Length == 2)
                                                                    {
                                                                        totalCount = arrayCount[0].ToString();
                                                                        currCount = arrayCount[1].ToString();
                                                                    }

                                                                    //设置滑块与滚动条布局
                                                                    if (listindex == 1)
                                                                    {
                                                                        objfw1 = FindeComponetByList(grid2List, "rdsp_scrollBar1", eComtmp.Pctxid);
                                                                    }
                                                                    if (listindex == 2)
                                                                    {
                                                                        objfw1 = FindeComponetByList(grid2List, "rdsp_scrollBar2", eComtmp.Pctxid);
                                                                    }
                                                                    if (listindex == 3)
                                                                    {
                                                                        objfw1 = FindeComponetByList(grid2List, "seep_scrollBar1", eComtmp.Pctxid);
                                                                    }
                                                                    if (listindex == 4)
                                                                    {
                                                                        objfw1 = FindeComponetByList(grid2List, "seep_scrollBar2", eComtmp.Pctxid);
                                                                    }
                                                                    if (listindex == 5)
                                                                    {
                                                                        objfw1 = FindeComponetByList(grid2List, "mhdp_scrollBar1", eComtmp.Pctxid);
                                                                    }
                                                                    if (listindex == 6)
                                                                    {
                                                                        objfw1 = FindeComponetByList(grid2List, "mhdp_scrollBar2", eComtmp.Pctxid);
                                                                    }

                                                                    ScrollBar sb = (ScrollBar)objfw1;
                                                                    sb.Maximum = Int32.Parse(totalCount) - Int32.Parse(ContractData.listViewCount[listindex.ToString()]);
                                                                    ContractData.listIndex[listindex.ToString()] = currCount.ToString();
                                                                    sb.Value = Int32.Parse(currCount);
                                                                    ContractData.listTotalCount[listindex.ToString()] = sb.Maximum.ToString();
                                                                    break;
                                                                }

                                                            }
                                                            //dttmp.Rows.Clear();
                                                            curCount = 0;
                                                            continue;
                                                        }
                                                        if (array[i].ToString().Contains("data2") && array[i].ToString().Contains("end"))  //是该组报文的结尾
                                                        {
                                                            ContractData.listCurCount[listindex.ToString()] = "0";
                                                            while (dttmp.Rows.Count > curCount)  //删除上一页多余行
                                                            {
                                                                dttmp.Rows.Remove(dttmp.Rows[dttmp.Rows.Count - 1]);
                                                            }

                                                            continue;
                                                        }

                                                        if (dttmp.Rows.Count <= 0)  //datatable没有数据
                                                        {

                                                            dttmp.Rows.InsertAt(Json2DataTableSingle(array[i], dttmp), dttmp.Rows.Count);
                                                            curCount++;
                                                        }
                                                        else
                                                        {
                                                            if (dttmp.Rows.Count > curCount)  //更新数据
                                                            {
                                                                //DataRow newRow = Json2DataTableSingle(array[i], dttmp);
                                                                //UpdateRow(dttmp, newRow, curCount);
                                                                JsonUpdateTableSingle(array[i], dttmp.Rows[curCount]);
                                                            }
                                                            else    //插入数据
                                                            {
                                                                dttmp.Rows.InsertAt(Json2DataTableSingle(array[i], dttmp), curCount);
                                                            }
                                                            curCount++;
                                                        }
                                                        if (listindex != 0)   //将最新的数据表count保存
                                                        {
                                                            ContractData.listCurCount[listindex.ToString()] = curCount.ToString();
                                                        }
                                                        //info = "解析一条数据完成：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                                                        //ContractData.logInstance.Debug(info);

                                                    }
                                                    dttmp.EndLoadData();

                                                    (objfw as DataGrid).ItemsSource = dttmp.DefaultView;
                                                    dicsDataTables[realpath + eComtmp.Pctxid] = dttmp;
                                                    return;

                                                }

                                                if (objfw != null && realpath == "cotp/dlslst" || realpath == "cotp/dsllst" || realpath == "cotp/ndslst")
                                                {
                                                    DataTable dttmp = new DataTable();
                                                    dttmp.Rows.InsertAt(Jsonstr2DataTableSingle(eComtmp.Value, dttmp), rowIdx);
                                                    (objfw as DataGrid).ItemsSource = dttmp.DefaultView;
                                                    dicsDataTables.Add(realpath + eComtmp.Pctxid, dttmp);
                                                }

                                                if (objfw is kmipsh)
                                                {
                                                    try
                                                    {
                                                        string jsonstr = "[" + eComtmp.Value + "]";
                                                        DataTable dt2 = Jsonstr2DataTable(eComtmp);

                                                        foreach (DataRow dr in dt2.Rows)
                                                        {
                                                            string martim = (string)dr["martim"];
                                                            string mardat = (string)dr["mardat"];
                                                            string concod = (string)dr["concod"];
                                                            string higpri = (string)dr["higpri"];
                                                            string lowpri = (string)dr["lowpri"];
                                                            string opnpri = (string)dr["opepri"];
                                                            string trdnum = (string)dr["deanum"];

                                                            if (higpri != "" && lowpri != "" && concod != "" && trdnum != "" && martim != "")
                                                            {
                                                                ((kmipsh)objfw).fenshitu(
                                                                    (string)concod, (string)higpri, (string)opnpri,
                                                                    "", "", (string)lowpri, (string)trdnum,
                                                                    (string)mardat, (string)martim);
                                                            }
                                                        }
                                                        lock (rapidMap)
                                                        {
                                                            if (!rapidMap.ContainsKey(realpath + eComtmp.Pctxid))
                                                                rapidMap.Add(realpath + eComtmp.Pctxid, objfw);
                                                        }
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        ContractData.ServerLogger.Warn(ex.ToString());
                                                    }
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            ContractData.ServerLogger.Warn(ex.ToString());
                                        }
                                    }
                                }));
                                continue;
                            }
                            if (eComtmp.Uri == "tbap/mstlst")
                            {
                                try
                                {
                                    lock (lockobj)
                                    {
                                        DataTable dt = Jsonstr2DataTable(eComtmp);
                                        // ContractData.constaData.
                                        if (ContractData.constaData.Rows.Count > 0)
                                        {
                                            for (int i = 0; i < dt.Rows.Count; i++)
                                            {
                                                string key = (string)dt.Rows[i]["tittle"];
                                                string inr = (string)dt.Rows[i]["inr"];
                                                string content = (string)dt.Rows[i]["contex"];
                                                DataRow[] rows = ContractData.constaData.Select("tittle='" + key + "'");
                                                if (rows.Length > 0)
                                                {
                                                    string oinr = (string)rows[0]["inr"];
                                                    if (Int32.Parse(inr) > Int32.Parse(oinr))
                                                    {
                                                        rows[0]["inr"] = inr;
                                                        rows[0]["tittle"] = key;
                                                        rows[0]["contex"] = content;
                                                        //  JsonstrUpdateTableSingle(eComtmp.Value, rows[0]); //做更新
                                                    }

                                                }
                                                else
                                                {
                                                    DataRow row = ContractData.constaData.NewRow();
                                                    row["inr"] = inr;
                                                    row["tittle"] = key;
                                                    row["contex"] = content;
                                                    ContractData.constaData.Rows.InsertAt(row, 0);
                                                }
                                            }


                                        }
                                        else
                                            ContractData.constaData = dt; //初始化内容
                                        continue;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    ContractData.ServerLogger.Warn(ex.ToString());
                                }
                            }
                            if ((eComtmp.Uri == "sysmod/usr/trddat" ||
                                eComtmp.Uri == "mifp/miflst" ||
                                eComtmp.Uri == "rdsp/cptlst" ||
                                eComtmp.Uri == "rdsp/ccmlst" ||
                                eComtmp.Uri == "mhdp/mhdlst" ||
                                eComtmp.Uri == "mhdp/mselst" ||
                                eComtmp.Uri == "sdep/seelst" ||
                                eComtmp.Uri == "sdep/sdelst" ||
                                eComtmp.Uri == "cotp/cotlst" ||
                                eComtmp.Uri == "cotp/dlslst" ||
                                eComtmp.Uri == "cotp/dsllst"

                                ))
                            {
                                if (eComtmp.Uri == "sysmod/usr/trddat")
                                {
                                    string timstr = eComtmp.Value;
                                    try
                                    {
                                        timstr = timstr.Insert(4, "-");
                                        timstr = timstr.Insert(7, "-");
                                        kmipsh.sysdate = DateTime.Parse(timstr);
                                    }
                                    catch(Exception ex)
                                    {
                                        ContractData.ServerLogger.Warn(ex.ToString());
                                    }
                                }
                                //快速处理
                                lock (rapidMap)
                                {
                                    if (rapidMap.ContainsKey(eComtmp.Uri + eComtmp.Pctxid))
                                        objfw = rapidMap[eComtmp.Uri + eComtmp.Pctxid];

                                }
                                if (objfw == null)
                                {
                                    if (ctxmap.ContainsKey(eComtmp.Uri + eComtmp.Pctxid))
                                        objfw = ctxmap[eComtmp.Uri + eComtmp.Pctxid];
                                    else
                                    {
                                        objfw = FindeComponetByList(grid2List, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                                        ctxmap.Add(eComtmp.Uri + eComtmp.Pctxid, objfw);
                                    }
                                }

                            }
                            else
                            {
                                if (ctxmap.ContainsKey(eComtmp.Uri + eComtmp.Pctxid))
                                    objfw = ctxmap[eComtmp.Uri + eComtmp.Pctxid];
                                else
                                {
                                    objfw = FindeComponetByList(grid2List, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                                    ctxmap.Add(eComtmp.Uri + eComtmp.Pctxid, objfw);
                                }
                            }
                            if (objfw == null)
                            {
                                //对此单选框做特殊处理
                                if (eComtmp.Uri == "cdsp/zt")
                                {
                                    string realUri = "s" + eComtmp.Value;
                                    objfw = FindeComponetByList(grid2List, realUri.Replace("/", "_"), eComtmp.Pctxid);
                                    objfw.Dispatcher.Invoke(new Action(() =>
                                    {
                                        ((RadioButton)objfw).IsChecked = true;

                                    }));
                                }


                                continue;
                            }
                            else if (objfw is kmipsh)
                            {

                            }
                            else if (objfw is TextBox)
                            {
                                new Thread(new ThreadStart(delegate
                                {
                                    objfw.Dispatcher.Invoke(new Action(() =>
                                    {
                                        ((TextBox)objfw).Text = eComtmp.Value;
                                    }));
                                })).Start();
                                // return;
                            }
                            else if (objfw is Button)
                            {
                                objfw.Dispatcher.Invoke(new Action(() =>
                                {
                                    ((Button)objfw).Content = eComtmp.Value;
                                }));

                                // return;
                            }
                            else if (objfw is ToggleSwitchButton)
                            {
                                objfw.Dispatcher.Invoke(new Action(() =>
                                {
                                    if (eComtmp.Value == "X")
                                    {
                                        ((ToggleSwitchButton)objfw).IsChecked = true;
                                        //IsChecked = true;
                                    }
                                    else
                                    {
                                        ((ToggleSwitchButton)objfw).IsChecked = false;
                                    }
                                }));
                            }
                            else if (objfw is DataGrid)
                            {
                                try
                                {
                                    if (eComtmp.Value == "")
                                    {
                                        objfw.Dispatcher.Invoke(new Action(() =>
                                        {
                                            ((DataGrid)objfw).ItemsSource = null;
                                        }));
                                        continue;
                                        // return;
                                    }

                                    int datagridindex = 0;
                                    objfw.Dispatcher.Invoke(new Action(() =>
                                    {
                                        datagridindex = ((DataGrid)objfw).SelectedIndex;
                                        if (dicsDataTables.ContainsKey(eComtmp.Uri + eComtmp.Pctxid))
                                        {
                                            DataTable dttmp = Jsonstr2DataTable(eComtmp);

                                            dicsDataTables[eComtmp.Uri + eComtmp.Pctxid] = dttmp;
                                            ((DataGrid)objfw).ItemsSource = dttmp.DefaultView;

                                        }
                                        else
                                        {
                                            DataTable dttmp = Jsonstr2DataTable(eComtmp);
                                            ((DataGrid)objfw).ItemsSource = dttmp.DefaultView;
                                            dicsDataTables.Add(eComtmp.Uri + eComtmp.Pctxid, dttmp);
                                        }
                                        ((DataGrid)objfw).SelectedIndex = datagridindex;
                                    }));
                                    eComponet ecom = new eComponet();
                                    ecom.Action = "OnClick";
                                    ecom.Uri = eComtmp.Uri;
                                    ecom.Value = datagridindex.ToString();
                                    ecom.Pctxid = eComtmp.Pctxid;
                                    InterFaceRPCStatic.thriftRPCall(ecom);
                                }
                                catch (Exception ex)
                                {
                                    ContractData.ServerLogger.Warn(ex.ToString());
                                }
                            }
                            else if (objfw is ListView)
                            {
                                if (eComtmp.Value == "")
                                {
                                    objfw.Dispatcher.Invoke(new Action(() =>
                                    {
                                        ((ListView)objfw).ItemsSource = null;
                                    }));
                                    continue;
                                    //return;
                                }
                                List<List<string>> listviewdata = new List<List<string>>();
                                List<string> listHead = new List<string>();
                                bool isGetHead = false;
                                objfw.Dispatcher.Invoke(new Action(() =>
                                {
                                    if ((((ListView)objfw).View as GridView).Columns.Count > 0)
                                        isGetHead = true;
                                }));

                                var jalist = JArray.Parse(eComtmp.Value);

                                foreach (JObject jo in jalist)
                                {
                                    List<string> itemList = new List<string>();
                                    foreach (var item in jo)
                                    {
                                        if (!isGetHead)
                                            listHead.Add(item.Key);
                                        itemList.Add(item.Value.ToString());
                                    }
                                    isGetHead = true;
                                    listviewdata.Add(itemList);
                                }

                                objfw.Dispatcher.Invoke(new Action(() =>
                                {
                                    ((ListView)objfw).ItemsSource = listviewdata;
                                    //((DataGrid)objfw).ItemsSource = listviewdata;
                                }));

                                foreach (string headstr in listHead)
                                {
                                    objfw.Dispatcher.Invoke(new Action(() =>
                                    {
                                        GridViewColumn columnC = new GridViewColumn();
                                        GridViewColumnHeader columnHead = new GridViewColumnHeader();

                                        columnHead.Content = headstr;
                                        columnHead.Tag = headstr;
                                        columnC.Header = columnHead;

                                        columnC.DisplayMemberBinding = new Binding(".[" + listHead.IndexOf(headstr).ToString() + "]");



                                        (((ListView)objfw).View as GridView).Columns.Add(columnC);
                                    }));

                                }

                            }
                            else if (objfw is ComboBox)
                            {

                                objfw.Dispatcher.Invoke(new Action(() =>
                                {
                                    foreach (ComboBoxItem cbi in ((ComboBox)objfw).Items)
                                    {
                                        string ctp = "";
                                        if (cbi.ToolTip != null)
                                            ctp = cbi.ToolTip.ToString();
                                        if (ctp == "-1" && eComtmp.Value == "")
                                        {
                                            ((ComboBox)objfw).SelectedItem = cbi;
                                            break;
                                        }
                                        if (ctp == eComtmp.Value)
                                        {
                                            ((ComboBox)objfw).SelectedItem = cbi;
                                            break;
                                        }
                                    }

                                }));
                            }
                            else if (eComtmp.Uri == "cdsp/zt")
                            {
                                string realUri = eComtmp.Uri + "/" + eComtmp.Value;
                                objfw = FindeComponetByList(grid2List, realUri.Replace("/", "_"), eComtmp.Pctxid);
                                objfw.Dispatcher.Invoke(new Action(() =>
                                {
                                    ((RadioButton)objfw).IsChecked = true;

                                }));
                            }
                            else
                            {
                                //  MessageBox.Show(eComtmp.Uri + " is not exist.");
                            }
                        }
                        else if (streve.StartsWith("setModify_APPEND"))
                        {
                            if (eComtmp.Uri == "tbap/mstlst")
                            {
                                try
                                {
                                    lock (lockobj)
                                    {
                                        DataTable dt = Jsonstr2DataTable(eComtmp);
                                        int index_tbap = pcxids.IndexOf(eComtmp.Pctxid);
                                        List<FrameworkElement> grid2List_tbap = feInitList[index_tbap];
                                        if (ContractData.constaData.Rows.Count > 0)
                                        {
                                            for (int i = 0; i < dt.Rows.Count; i++)
                                            {
                                                string key = (string)dt.Rows[i]["tittle"];
                                                string inr = (string)dt.Rows[i]["inr"];
                                                string content = (string)dt.Rows[i]["contex"];
                                                DataRow[] rows = ContractData.constaData.Select("tittle='" + key + "'");
                                                if (rows.Length > 0)
                                                {
                                                    string oinr = (string)rows[0]["inr"];
                                                    if (Int32.Parse(inr) > Int32.Parse(oinr))
                                                    {
                                                        rows[0]["inr"] = inr;
                                                        rows[0]["tittle"] = key;
                                                        rows[0]["contex"] = content;
                                                        //JsonstrUpdateTableSingle(eComtmp.Value, rows[0]); //做更新
                                                    }
                                                }
                                                else
                                                {
                                                    DataRow row = ContractData.constaData.NewRow();
                                                    row["inr"] = inr;
                                                    row["tittle"] = key;
                                                    row["contex"] = content;
                                                    ContractData.constaData.Rows.InsertAt(row, 0);

                                                }
                                            }


                                        }
                                        else
                                        {
                                            ContractData.constaData = dt; //初始化内容
                                            if (dt.Rows.Count > 0)
                                            {
                                                DataView dv = ContractData.constaData.DefaultView;
                                                dv.Sort = "contex";
                                                DataTable show_dt = dv.ToTable();
                                            }

                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    ContractData.ServerLogger.Warn(ex.ToString());
                                }
                                continue;
                            }
                            bool isRapid = false;
                            //DataGrid追加
                            int index = pcxids.IndexOf(eComtmp.Pctxid);
                            FrameworkElement objfw = null;
                            List<FrameworkElement> grid2List = feInitList[index];
                            if ((eComtmp.Uri == "sysmod/usr/trddat" ||
                                eComtmp.Uri == "mifp/miflst" ||
                               eComtmp.Uri == "rdsp/cptlst" ||
                               eComtmp.Uri == "rdsp/ccmlst" ||
                               eComtmp.Uri == "mhdp/mhdlst" ||
                               eComtmp.Uri == "mhdp/mselst" ||
                               eComtmp.Uri == "sdep/seelst" ||
                               eComtmp.Uri == "sdep/sdelst" ||
                               eComtmp.Uri == "cotp/cotlst" ||
                               eComtmp.Uri == "cotp/dlslst" ||
                               eComtmp.Uri == "cotp/dsllst"
                               ) && rapidMap.ContainsKey(eComtmp.Uri + eComtmp.Pctxid))
                            {
                                if (eComtmp.Uri == "sysmod/usr/trddat")
                                {
                                    string timstr = eComtmp.Value;
                                    try
                                    {
                                        timstr = timstr.Insert(4, "-");
                                        timstr = timstr.Insert(7, "-");
                                        kmipsh.sysdate = DateTime.Parse(timstr);
                                    }
                                    catch(Exception ex)
                                    {
                                        ContractData.ServerLogger.Warn(ex.ToString());
                                    }
                                }
                                //快速处理
                                lock (rapidMap)
                                {
                                    objfw = rapidMap[eComtmp.Uri + eComtmp.Pctxid];
                                    isRapid = true;
                                }
                                if (objfw == null)
                                {
                                    if (ctxmap.ContainsKey(eComtmp.Uri + eComtmp.Pctxid))
                                        objfw = ctxmap[eComtmp.Uri + eComtmp.Pctxid];
                                    else
                                    {
                                        objfw = FindeComponetByList(grid2List, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                                        ctxmap.Add(eComtmp.Uri + eComtmp.Pctxid, objfw);
                                    }
                                }
                            }
                            else
                            {
                                if (ctxmap.ContainsKey(eComtmp.Uri + eComtmp.Pctxid))
                                    objfw = ctxmap[eComtmp.Uri + eComtmp.Pctxid];
                                else
                                {
                                    objfw = FindeComponetByList(grid2List, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                                    ctxmap.Add(eComtmp.Uri + eComtmp.Pctxid, objfw);
                                }

                            }
                            if (objfw is kmipsh)
                            {
                                //分时图数据批量合并
                                objfw.Dispatcher.Invoke(new Action(() =>
                                {
                                    (objfw as kmipsh).importPLData(eComtmp.Value);
                                }));
                                continue;
                            }

                            // FrameworkElement objfw = FindeComponetByList(grid2List, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                            DataGrid gird = objfw as DataGrid;
                            if (gird == null)
                                continue;
                            objfw.Dispatcher.Invoke(new Action(() =>
                            {
                                lock (objfw)
                                {
                                    if (dicsDataTables.ContainsKey(eComtmp.Uri + eComtmp.Pctxid))
                                    {
                                        try
                                        {
                                            string pagestr = streve.Substring("setModify_APPEND".Length);
                                            int pageno = int.Parse(pagestr);
                                            if (pageno == 0)
                                            {

                                                try
                                                {
                                                    if (isRapid)
                                                    {
                                                        JArray array = JsonConvert.DeserializeObject(eComtmp.Value) as JArray;//反序列化为数组
                                                        DataTable source = null;

                                                        source = dicsDataTables[eComtmp.Uri + eComtmp.Pctxid];
                                                        JsonstrAppendDataTable2(array, source);


                                                    }
                                                    else if (eComtmp.Uri == "cdsp/cdslst")//合约状态维护
                                                    {
                                                        DataTable dttmp = Jsonstr2DataTable(eComtmp);
                                                        if (!dttmp.Columns.Contains("InstCode"))
                                                        {
                                                            dttmp.Columns.Add("InstCode");
                                                        }
                                                        foreach (DataRow item in dttmp.Rows)
                                                        {
                                                            item["InstCode"] = InstModelCollection.GetInstCode(item["concod"].ToString());
                                                        }
                                                        dicsDataTables[eComtmp.Uri + eComtmp.Pctxid] = dttmp;
                                                        ((DataGrid)objfw).ItemsSource = dttmp.DefaultView;
                                                    }
                                                    else
                                                    {
                                                        int datagridindex = ((DataGrid)objfw).SelectedIndex;
                                                        DataTable dttmp = Jsonstr2DataTable(eComtmp);

                                                        dicsDataTables[eComtmp.Uri + eComtmp.Pctxid] = dttmp;
                                                        ((DataGrid)objfw).ItemsSource = dttmp.DefaultView;
                                                    }
                                                }
                                                catch (Exception ex){ ContractData.ServerLogger.Warn(ex.ToString()); }
                                                //(DataGrid)objfw).ItemsSource = dttmp.DefaultView;
                                                // ((DataGrid)objfw).SelectedIndex = datagridindex;

                                            }
                                            else
                                            {
                                                JArray array = JsonConvert.DeserializeObject(eComtmp.Value) as JArray;//反序列化为数组

                                                try
                                                {
                                                    int datagridindex = ((DataGrid)objfw).SelectedIndex;
                                                    DataTable source = null;

                                                    source = dicsDataTables[eComtmp.Uri + eComtmp.Pctxid];
                                                    JsonstrAppendDataTable2(array, source);
                                                }
                                                catch(Exception ex)
                                                {
                                                    ContractData.ServerLogger.Warn(ex.ToString());
                                                    return;
                                                }

                                                // ((DataGrid)objfw).SelectedIndex = datagridindex;

                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            ContractData.ServerLogger.Warn(ex.ToString());
                                        }
                                    }
                                    else
                                    {

                                        try
                                        {
                                            int index2 = InterFaceRPCStatic.pcxids.LastIndexOf(eComtmp.Pctxid);
                                            if (index2 < 0)
                                                return;
                                            int datagridindex = ((DataGrid)objfw).SelectedIndex;
                                            DataTable dttmp = Jsonstr2DataTable(eComtmp);

                                            ((DataGrid)objfw).ItemsSource = dttmp.DefaultView;
                                            //临时补丁重新设置condet中的datagrid的Rowfilter 
                                            if (eComtmp.Uri == "cotp/cotlst")
                                            {
                                                RefilterCondetItems((DataGrid)objfw);
                                            }
                                            else if (eComtmp.Uri== "cdsp/cdslst")//合约状态维护
                                            {
                                                if (!dttmp.Columns.Contains("InstCode"))
                                                {
                                                    dttmp.Columns.Add("InstCode");
                                                }
                                                foreach (DataRow item in dttmp.Rows)
                                                {
                                                    item["InstCode"] = InstModelCollection.GetInstCode(item["concod"].ToString());
                                                }
                                            }
                                            // if (ContractData.version == "yellow")
                                            {
                                                //应急初始化过滤器

                                            }
                                            lock (dicsDataTables)
                                            {
                                                if (dicsDataTables.ContainsKey(eComtmp.Uri + eComtmp.Pctxid))
                                                {
                                                    dicsDataTables[eComtmp.Uri + eComtmp.Pctxid].Merge(dttmp);
                                                }
                                                else
                                                    dicsDataTables.Add(eComtmp.Uri + eComtmp.Pctxid, dttmp);
                                            }
                                        }
                                        catch (Exception ex){ ContractData.ServerLogger.Warn(ex.ToString()); }
                                        //((DataGrid)objfw).SelectedIndex = datagridindex;

                                    }
                                }

                                //合约状态切换的单独处理
                                bool isSctsme = false;
                                Grid sct_grid = grid2List[0] as Grid;
                                sctsme sct = null;
                                if (sct_grid != null)
                                {
                                    sct = sct_grid.Children[0] as sctsme;
                                    if (sct != null)
                                    {
                                        sct.refreshState();
                                    }
                                }

                            }));
                        }
                        else if (streve == "setStyle")
                        {
                            eComponetEventHandleMultiStyle(eComtmp);
                            continue;
                        }
                        else if (streve == "disable")
                        {
                            FrameworkElement objfw = FindeComponet(dp, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                            //PrintLogicalTree(0, grid1);
                            //MessageBox.Show(rtnstr);
                            if (objfw != null)
                            {
                                //调试event
                                //MessageBox.Show("event无法处理:" + eComtmp.Uri);
                                objfw.Dispatcher.Invoke(new Action(() =>
                                {
                                    objfw.IsEnabled = false;
                                }));
                            }


                        }
                        else if (streve == "enable")
                        {
                            ContractData.ClientLogger.Debug("-----将按钮恢复高亮！-----");
                            FrameworkElement objfw = FindeComponet(dp, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                            //PrintLogicalTree(0, grid1);
                            //MessageBox.Show(rtnstr);
                            if (objfw != null)
                            {
                                //调试event
                                //MessageBox.Show("event无法处理:" + eComtmp.Uri);
                                objfw.Dispatcher.Invoke(new Action(() =>
                                {
                                    objfw.IsEnabled = true;
                                }));
                            }


                        }
                        else if (streve == "direct")
                        {
                            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                            {
                                if (!panelNames.Contains(eComtmp.Uri.Replace("/", "_")))
                                {
                                    LayoutAnchorable anchorable = new LayoutAnchorable();
                                    anchorable.ContentId = eComtmp.Pctxid;
                                    Grid grid2 = new Grid();
                                    object xamlUI = Activator.CreateInstance(Type.GetType("shgoldexchange." + eComtmp.Value), false);
                                    panelNames.Add(eComtmp.Uri.Replace("/", "_"));


                                    pcxids.Add(eComtmp.Pctxid);
                                    List<FrameworkElement> grid2List = new List<FrameworkElement>();
                                    grid2List.Add(grid2);
                                    feInitList.Add(grid2List);
                                    grid2.Children.Add(xamlUI as UserControl);
                                    SetTagComponet(grid2, typeof(Button), eComtmp.Pctxid);
                                    SetTagComponet(grid2, typeof(ListView), eComtmp.Pctxid);
                                    SetTagComponet(grid2, typeof(TextBox), eComtmp.Pctxid);
                                    SetTagComponet(grid2, typeof(DataGrid), eComtmp.Pctxid);
                                    SetTagComponet(grid2, typeof(PasswordBox), eComtmp.Pctxid);
                                    anchorable.Content = grid2;
                                    anchorable.FloatingWidth = 600;
                                    anchorable.FloatingHeight = 300;
                                    anchorable.FloatingTop = 200;
                                    anchorable.FloatingLeft = 300;
                                    closeids.Add(eComtmp.Pctxid);
                                    listDp.Add(anchorable);
                                    ldp.Children.Add(anchorable);
                                    anchorable.Closing += anchorable_Closing;
                                    anchorable.Float();     //调用Float方法，使窗体浮动显示
                                }
                                else
                                {
                                    ldp.Children[panelNames.IndexOf(eComtmp.Uri.Replace("/", "_"))].Float();
                                }


                            }));
                        }
                        else if (streve == "ClosePanel")
                        {
                            try
                            {
                                if (eComtmp.Uri != "" && panelNames.Contains(eComtmp.Uri.Replace("/", "_")))
                                {

                                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                                    {
                                        //listDp[panelNames.IndexOf(eComtmp.Uri.Replace("/", "_"))].DockAsDocument();
                                        //listDpW[0].Close();
                                        int index = pcxids.IndexOf(eComtmp.Pctxid);
                                        index = panelNames.IndexOf(eComtmp.Uri.Replace("/", "_"));
                                        System.Windows.Application.Current.MainWindow.Focus();// MainWindow.Activate();
                                        System.Windows.Application.Current.MainWindow.Activate();
                                        try
                                        {
                                            if (listDp[index] == null) return;
                                        }
                                        catch (Exception ex)
                                        {
                                            ContractData.ServerLogger.Warn(ex.ToString());
                                            return;
                                        }
                                        listDp[index].Close();
                                        listDp.RemoveAt(index);
                                        panelNames.RemoveAt(index);
                                        closeids.RemoveAt(index);
                                        lock (dicsDataTables)
                                        {
                                            foreach (string key in dicsDataTables.Keys)
                                            {
                                                if (key.Contains(eComtmp.Pctxid))
                                                {
                                                    dicsDataTables[key].Clear();
                                                    dicsDataTables.Remove(key);
                                                }
                                            }
                                        }
                                        lock (rapidMap)
                                        {
                                            foreach (string key in rapidMap.Keys)
                                            {
                                                if (key.Contains(eComtmp.Pctxid))
                                                    rapidMap.Remove(key);
                                            }
                                        }
                                        System.Windows.Application.Current.MainWindow.Focus();// MainWindow.Activate();
                                        System.Windows.Application.Current.MainWindow.Activate();
                                    }));

                                }
                                else
                                {
                                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                                    {
                                        //listDp[panelNames.IndexOf(eComtmp.Uri.Replace("/", "_"))].DockAsDocument();
                                        //listDpW[0].Close();
                                        int index = closeids.LastIndexOf(eComtmp.Pctxid);
                                        System.Windows.Application.Current.MainWindow.Focus();// MainWindow.Activate();
                                        System.Windows.Application.Current.MainWindow.Activate();
                                        try
                                        {
                                            if (listDp[index] == null) return;
                                        }
                                        catch (Exception ex)
                                        {
                                            ContractData.ServerLogger.Warn(ex.ToString());
                                            return;
                                        }
                                        listDp[index].Close();
                                        listDp.RemoveAt(index);
                                        panelNames.RemoveAt(index);
                                        closeids.RemoveAt(index);

                                        lockids.RemoveAt(index);
                                        int index2 = pcxids.IndexOf(eComtmp.Pctxid);
                                        pcxids.RemoveAt(index2);
                                        feInitList.RemoveAt(index2);
                                        activeTrans.RemoveAt(index2);
                                        lock (dicsDataTables)
                                        {
                                            foreach (string key in new List<string>(dicsDataTables.Keys))
                                            {
                                                if (key.Contains(eComtmp.Pctxid))
                                                {
                                                    dicsDataTables[key].Clear();
                                                    dicsDataTables.Remove(key);

                                                }
                                            }
                                        }
                                        lock (rapidMap)
                                        {
                                            foreach (string key in new List<string>(rapidMap.Keys))
                                            {
                                                if (key.Contains(eComtmp.Pctxid))
                                                    rapidMap.Remove(key);
                                            }
                                        }
                                        System.Windows.Application.Current.MainWindow.Focus();// MainWindow.Activate();
                                        System.Windows.Application.Current.MainWindow.Activate();
                                    }));
                                }
                            }
                            catch (Exception ex)
                            {
                                ContractData.ServerLogger.Warn(ex.ToString());
                                return;
                            }
                        }
                        else if (streve == "OnClick")
                        {
                            try
                            {
                                string tmpUri = eComtmp.Uri;
                                string transUriName = tmpUri.Substring(0, tmpUri.IndexOf(":"));
                                eComtmp.Uri = tmpUri.Substring(tmpUri.IndexOf(":") + 1);
                                if (eComtmp.Pctxid == "")   //如果pctxid不为空，则应该由指定具体的Pctxid
                                    eComtmp.Pctxid = pcxids[activeTrans.IndexOf(transUriName.Trim())];
                                int index = pcxids.IndexOf(eComtmp.Pctxid);
                                if (index < 0)
                                    continue;
                                //System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                                //{
                                List<FrameworkElement> grid2List = feInitList[index];
                                FrameworkElement objfw = FindeComponetByList(grid2List, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                                if (objfw != null)
                                {
                                    if (objfw is DataGrid)
                                    {
                                        eComtmp.Value = "-1";
                                        objfw.Dispatcher.Invoke(new Action(() =>
                                        {
                                            DataTable table = dicsDataTables[eComtmp.Uri + eComtmp.Pctxid];
                                            if ((((DataGrid)objfw).SelectedItem == null))
                                                return;
                                            System.Data.DataRowView row = (((DataGrid)objfw).SelectedItem) as System.Data.DataRowView;
                                            if (row == null)
                                                return;
                                            eComtmp.Value = row.Row.Table.Rows.IndexOf(row.Row).ToString();
                                            // eComtmp.Value = ((DataGrid)objfw).SelectedIndex.ToString();
                                        }));
                                    }
                                    objfw.Dispatcher.Invoke(new Action(() =>
                                    {
                                        if (((Grid)grid2List[0]).Children.Count > 0)
                                        {
                                            sctsme sct = ((Grid)grid2List[0]).Children[0] as sctsme;
                                            if (sct != null)
                                            {
                                                sct.checkboxAll.IsChecked = false;
                                                sct.clearRBCheck();
                                            }
                                        }
                                    }
                                    ));
                                }

                                //Thread.Sleep(2000);
                                Thread eventThread;
                                eventThread = new Thread(new ParameterizedThreadStart(callthrift));
                                eventThread.SetApartmentState(ApartmentState.STA);
                                eventThread.IsBackground = true;
                                eventThread.Start(eComtmp);
                            }
                            catch (Exception ex)
                            {
                                ContractData.ServerLogger.Warn(ex.ToString());
                                //    MessageBox.Show(eeee.Message);
                            }
                        }
                        else if (streve == "ERROR")
                        {
                            try
                            {
                                int hintindex = textHint.IndexOf(eComtmp.Uri.Replace("/", "_"));
                                if (hintindex < 0)
                                {
                                    FrameworkElement objfw = FindeComponet(dp, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                                    //PrintLogicalTree(0, grid1);
                                    //MessageBox.Show(rtnstr);
                                    if (objfw == null)
                                    {
                                        //调试event
                                        //MessageBox.Show("event无法处理:" + eComtmp.Uri);
                                        // return;
                                        continue;
                                    }
                                    //rtnstr = "";
                                    if (objfw is TextBox)
                                    {
                                        objfw.Dispatcher.Invoke(new Action(() =>
                                        {
                                            //hintDiag hd = new hintDiag();
                                            //hd.ShowInTaskbar = false;
                                            //hintDiags.Add(hd);

                                            ////Point pt  = ((TextBox)objfw).TranslatePoint(new Point(0, 0), (UIElement)dp);
                                            //Point pt = ((TextBox)objfw).PointToScreen(new Point(0, 0));
                                            if (eComtmp.Value == "__CLEAR__")
                                            {
                                                ((TextBox)objfw).SetResourceReference(TextBox.StyleProperty, "styleTextBoxTrade");

                                                ((TextBox)objfw).ToolTip = null;
                                            }
                                            else
                                            {
                                                ((TextBox)objfw).SetResourceReference(TextBox.StyleProperty, "textRed");
                                                //hd.Left = pt.X + ((TextBox)objfw).Width;
                                                //hd.Top = pt.Y;
                                                //textHint.Add(eComtmp.Uri.Replace("/", "_"));
                                                //hd.showText(eComtmp.Value);

                                                ((TextBox)objfw).ToolTip = eComtmp.Value;
                                            }

                                        }));
                                    }
                                    if (objfw is PasswordBox)
                                    {
                                        objfw.Dispatcher.Invoke(new Action(() =>
                                        {
                                            //hintDiag hd = new hintDiag();
                                            //hd.ShowInTaskbar = false;
                                            //hintDiags.Add(hd);

                                            ////Point pt  = ((TextBox)objfw).TranslatePoint(new Point(0, 0), (UIElement)dp);
                                            //Point pt = ((TextBox)objfw).PointToScreen(new Point(0, 0));
                                            if (eComtmp.Value == "__CLEAR__")
                                            {
                                                ((PasswordBox)objfw).SetResourceReference(PasswordBox.StyleProperty, "stylePasswordBox");

                                                ((PasswordBox)objfw).ToolTip = null;
                                            }
                                            else
                                            {
                                                ((PasswordBox)objfw).SetResourceReference(PasswordBox.StyleProperty, "stylePasswordRedBox");
                                                //hd.Left = pt.X + ((TextBox)objfw).Width;
                                                //hd.Top = pt.Y;
                                                //textHint.Add(eComtmp.Uri.Replace("/", "_"));
                                                //hd.showText(eComtmp.Value);

                                                ((PasswordBox)objfw).ToolTip = eComtmp.Value;
                                            }

                                        }));
                                    }
                                    else if (objfw is ComboBox)
                                    {
                                        objfw.Dispatcher.Invoke(new Action(() =>
                                        {
                                            //hintDiag hd = new hintDiag();
                                            //hd.ShowInTaskbar = false;
                                            //hintDiags.Add(hd);

                                            ////Point pt  = ((TextBox)objfw).TranslatePoint(new Point(0, 0), (UIElement)dp);
                                            //Point pt = ((TextBox)objfw).PointToScreen(new Point(0, 0));
                                            if (eComtmp.Value == "__CLEAR__")
                                            {
                                                ((ComboBox)objfw).SetResourceReference(ComboBox.StyleProperty, "styleComboBoxTrade");

                                                ((ComboBox)objfw).ToolTip = null;
                                            }
                                            else
                                            {
                                                ((ComboBox)objfw).SetResourceReference(ComboBox.StyleProperty, "styleComboBoxRed");
                                                //hd.Left = pt.X + ((TextBox)objfw).Width;
                                                //hd.Top = pt.Y;
                                                //textHint.Add(eComtmp.Uri.Replace("/", "_"));
                                                //hd.showText(eComtmp.Value);

                                                ((ComboBox)objfw).ToolTip = eComtmp.Value;
                                            }

                                        }));
                                    }



                                }
                                else
                                {
                                    FrameworkElement objfw = FindeComponet(dp, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                                    if (objfw == null)
                                        continue;
                                    objfw.Dispatcher.Invoke(new Action(() =>
                                    {
                                        //hintDiag hd = hintDiags[hintindex];
                                        //((TextBox)objfw).SetResourceReference(TextBox.StyleProperty, "textRed");
                                        //hd.Show();
                                    }));
                                }
                            }
                            catch (Exception ex)
                            {
                                ContractData.ServerLogger.Warn(ex.ToString());
                                return;
                            }
                        }
                        else if (streve == "FOCUS")
                        {
                            try
                            {
                                FrameworkElement objfw = FindeComponet(dp, eComtmp.Uri.Replace("/", "_"), eComtmp.Pctxid);
                                if (objfw == null)
                                {
                                    continue;
                                }
                                objfw.Dispatcher.Invoke(new Action(() =>
                                {
                                    objfw.Focus();
                                }));
                            }
                            catch (Exception ex)
                            {
                                ContractData.ServerLogger.Warn(ex.ToString());
                                return;
                            }
                        }
                        else if (streve == "SourcePull")
                        {
                            try
                            {
                                int count = 120;
                                int offset = 0;
                                while (true)
                                {
                                    eComponet ec = new eComponet();
                                    ec.Action = streve;
                                    ec.Pctxid = eComtmp.Pctxid;
                                    ec.Value = offset + "," + count;
                                    ec.Uri = eComtmp.Uri;
                                    servResp sr = helloClient.DoEvent(Usra, ec);
                                    if (sr != null && sr.Events != null && sr.Events.Count > 0)
                                    {
                                        eComponetEventHandle(sr.Events);
                                        offset += count;
                                        if (sr.ErrorMessage == "1")
                                        {
                                            break;
                                        }
                                    }
                                    else
                                        break;
                                }
                            }
                            catch (Exception ex)
                            {
                                ContractData.ServerLogger.Warn(ex.ToString());
                                return;
                            }
                        }
                        else
                        {
                            thriftRPCall(eComtmp);
                        }
                    }

                }
                catch (Exception ex)
                {
                    ContractData.ServerLogger.Warn(ex.ToString());
                }
            }
        }

        static void anchorable_Hiding(object sender, System.ComponentModel.CancelEventArgs e)
        {
            LayoutAnchorable anchorable = ((LayoutAnchorable)sender);
            //anchorable.DockAsDocument();


            eComponet ecom = new eComponet();
            ecom.Action = "ClosePanel";
            ecom.Uri = (panelNames[listDp.IndexOf(anchorable)]).Replace("_", "/");
            ecom.Value = lockids[listDp.IndexOf(anchorable)];
            ecom.Pctxid = anchorable.ContentId;
            InterFaceRPCStatic.thriftRPCall(ecom);
            // anchorable.Close();
        }

        private static void gridSizeChanged(object sender, SizeChangedEventArgs e)
        {
            Grid grid = (Grid)sender;
            double width = grid.ActualWidth;
            double height = grid.ActualHeight;
            if (grid.Children.Count > 0)
            {
                UserControl uc = (UserControl)grid.Children[0];
                uc.Width = width;
                uc.Height = height;
            }
        }

        public static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }

        static void anchorable_PopupClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            LayoutAnchorable anchorable = ((LayoutAnchorable)sender);
            //anchorable.DockAsDocument();
            try
            {

                eComponet ecom = new eComponet();
                ecom.Action = "ClosePanel";
                ecom.Uri = (panelNames[listDp.IndexOf(anchorable)]).Replace("_", "/");
                ecom.Value = lockids[listDp.IndexOf(anchorable)];
                ecom.Pctxid = anchorable.ContentId;
                InterFaceRPCStatic.thriftRPCall(ecom);
            }
            catch (Exception ex)
            {
                ContractData.ServerLogger.Warn(ex.ToString());
            }
            //e.Cancel = true;
        }

        static void anchorable_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            LayoutAnchorable anchorable = ((LayoutAnchorable)sender);
            //  anchorable.DockAsDocument();


            eComponet ecom = new eComponet();
            ecom.Action = "ClosePanel";
            ecom.Uri = (panelNames[listDp.IndexOf(anchorable)]).Replace("_", "/");
            ecom.Value = lockids[listDp.IndexOf(anchorable)];
            ecom.Pctxid = anchorable.ContentId;
            InterFaceRPCStatic.thriftRPCall(ecom);
            e.Cancel = true;
        }

        static private FrameworkElement FindeComponet(DependencyObject dp, Type elementType)
        {
            try
            {
                FrameworkElement rtnFrm;


                //递归调用每个逻辑子节点
                foreach (object child in LogicalTreeHelper.GetChildren(dp as DependencyObject))
                {
                    if (child is FrameworkElement && (child as FrameworkElement).GetType() == elementType)
                        return child as FrameworkElement;
                    if (!(child is DependencyObject))
                        continue;
                    rtnFrm = FindeComponet(child as DependencyObject, elementType);
                    if (rtnFrm == null)
                    {
                        continue;
                    }
                    else
                    {
                        return rtnFrm;
                    }
                }

                return null;
            }
            catch (Exception ex)
            {
                ContractData.ServerLogger.Warn(ex.ToString());
                //MessageBox.Show(ee.Message);
                return null;
            }
        }

        static List<FrameworkElement> FindeAllComponet(DependencyObject dp, Type elementType)
        {
            bool needNextLevel = true;
            //递归调用每个逻辑子节点
            foreach (object child in LogicalTreeHelper.GetChildren(dp as DependencyObject))
            {
                if (child is FrameworkElement && (child as FrameworkElement).GetType() == elementType)
                {
                    rtnFrms.Add(child as FrameworkElement);
                    needNextLevel = false;
                    continue;
                }
                if (!(child is DependencyObject))
                    continue;
                if (needNextLevel)
                    rtnFrms.Union(FindeAllComponet(child as DependencyObject, elementType));

            }

            return rtnFrms;
        }

        static public FrameworkElement FindeComponetByList(List<FrameworkElement> dp, string elementName, string pctxid)
        {
            if (elementName == "")
                return null;
            //watch.Restart();
            FrameworkElement rtnFrm = null;
            foreach (FrameworkElement fetmp in dp)
            {
                rtnFrm = FindeComponet(fetmp, elementName, pctxid);
                if (rtnFrm != null)
                    break;
            }
            //watch.Stop();
            //StreamWriter fs = new StreamWriter("aaa.txt", true);

            //fs.WriteLine(elementName+"--time:" + watch.ElapsedMilliseconds.ToString());

            //fs.Close();
            return rtnFrm;
        }

        static public FrameworkElement FindeComponet(DependencyObject dp, string elementName, string pctxid)
        {
            if (elementName == "")
                return null;
            FrameworkElement rtnFrm = null;
            try
            {
                //if (dp is FrameworkElement)
                //    rtnstr += (dp as FrameworkElement).GetType().ToString() + (dp as FrameworkElement).Name + Environment.NewLine;
                //递归调用每个逻辑子节点
                dp.Dispatcher.Invoke(new Action(() =>
                {
                    if (dp is FrameworkElement && (dp as FrameworkElement).Name == elementName && pctxid == (dp as FrameworkElement).Tag as string)
                    {
                        rtnFrm = dp as FrameworkElement;

                        return;
                    }
                    foreach (object child in LogicalTreeHelper.GetChildren(dp as DependencyObject))
                    {
                        if (child is FrameworkElement && (child as FrameworkElement).Name == elementName && pctxid == (child as FrameworkElement).Tag as string)
                        {
                            rtnFrm = child as FrameworkElement;

                            return;
                        }
                        if (!(child is DependencyObject))
                            continue;

                        rtnFrm = FindeComponet(child as DependencyObject, elementName, pctxid);
                        if (rtnFrm == null)
                        {
                            continue;
                        }
                        else
                        {
                            return;
                        }
                    }
                }));

            }
            catch (Exception ex)
            {
                ContractData.ServerLogger.Warn(ex.ToString());
            }

            return rtnFrm;
        }

        static public FrameworkElement FindeComponetNoID(DependencyObject dp, string elementName)
        {
            if (elementName == "")
                return null;
            FrameworkElement rtnFrm = null;

            //if (dp is FrameworkElement)
            //    rtnstr += (dp as FrameworkElement).GetType().ToString() + (dp as FrameworkElement).Name + Environment.NewLine;
            //递归调用每个逻辑子节点
            dp.Dispatcher.Invoke(new Action(() =>
            {
                if (dp is FrameworkElement && (dp as FrameworkElement).Name == elementName)
                {
                    rtnFrm = dp as FrameworkElement;

                    return;
                }
                foreach (object child in LogicalTreeHelper.GetChildren(dp as DependencyObject))
                {
                    if (child is FrameworkElement && (child as FrameworkElement).Name == elementName)
                    {
                        rtnFrm = child as FrameworkElement;

                        return;
                    }
                    if (!(child is DependencyObject))
                        continue;

                    rtnFrm = FindeComponetNoID(child as DependencyObject, elementName);
                    if (rtnFrm == null)
                    {
                        continue;
                    }
                    else
                    {
                        return;
                    }
                }
            }));

            return rtnFrm;
        }

        static private void SetTagComponet(DependencyObject dp, Type elementType, string strTag)
        {
            if (dp is FrameworkElement && (dp as FrameworkElement).GetType() == elementType)
            {
                (dp as FrameworkElement).Tag = strTag;
            }
            //递归调用每个逻辑子节点
            foreach (object child in LogicalTreeHelper.GetChildren(dp as DependencyObject))
            {
                if (child is FrameworkElement && (child as FrameworkElement).GetType() == elementType)
                {
                    (child as FrameworkElement).Tag = strTag;
                    continue;
                }
                if (!(child is DependencyObject))
                    continue;
                SetTagComponet(child as DependencyObject, elementType, strTag);
            }
        }

        public static DataGridRow GetDataGridRow(DataGrid datagrid, int rowIndex)
        {
            DataGridRow row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
            if (row == null)
            {
                datagrid.UpdateLayout();
                //datagrid.ScrollIntoView(datagrid.Items[rowIndex]);
                row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
                row.IsSelected = true;
            }
            return row;
        }


        private static void RefilterCondetItems(DataGrid datagrid)
        {
            //初始化盘口价显示挡位
            int priceSet = Int32.Parse(ContractData.priceString);
            if (priceSet > 0)
            {
                string filsql = "typ IN ('";
                if (priceSet >= 1)
                {
                    filsql += "买一价','卖一价','";
                }
                if (priceSet >= 2)
                {
                    filsql += "买二价','卖二价','";
                }
                if (priceSet >= 3)
                {
                    filsql += "买三价','卖三价','";
                }
                if (priceSet >= 4)
                {
                    filsql += "买四价','卖四价','";
                }
                if (priceSet >= 5)
                {
                    filsql += "买五价','卖五价','";
                }
                if (priceSet >= 6)
                {
                    filsql += "买六价','卖六价','";
                }
                if (priceSet >= 7)
                {
                    filsql += "买七价','卖七价','";
                }
                if (priceSet >= 8)
                {
                    filsql += "买八价','卖八价','";
                }
                if (priceSet >= 9)
                {
                    filsql += "买九价','卖九价','";
                }
                if (priceSet >= 10)
                {
                    filsql += "买十价','卖十价','";
                }
                if (filsql.EndsWith(",'"))
                {
                    filsql = filsql.Substring(0, filsql.Length - 2);
                    filsql += ")";
                }
                DataView dv = (DataView)datagrid.ItemsSource;
                if (dv != null)
                {
                    Console.WriteLine("JINGJIADEBUG___RowFilter");
                    ContractData.ClientLogger.Debug("DataView对象不为空，sql：" + filsql);
                    dv.RowFilter = filsql;
                }
                ContractData.ClientLogger.Debug("价格档位数据初始化成功！当前价格档位为：" + priceSet);
            }
        }



    }
}
